package com.xinghai.system.service.impl;

import java.util.List;
import com.xinghai.common.utils.DateUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import com.xinghai.system.mapper.DingUserMapper;
import com.xinghai.system.domain.DingUser;
import com.xinghai.system.service.IDingUserService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 钉钉组织-用户Service业务层处理
 * 
 * @author xinghai
 * @date 2024-07-23
 */
@Service
public class DingUserServiceImpl extends ServiceImpl<DingUserMapper, DingUser> implements IDingUserService
{
    @Autowired
    private DingUserMapper dingUserMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询钉钉组织-用户
     * 
     * @param id 钉钉组织-用户主键
     * @return 钉钉组织-用户
     */
    @Override
    public DingUser getInfo(String id)
    {
        return dingUserMapper.getInfo(id);
    }

    /**
     * 查询钉钉组织-用户列表
     * 
     * @param dingUser 钉钉组织-用户
     * @return 钉钉组织-用户
     */
    @Override
    public List<DingUser> list(DingUser dingUser)
    {
            dingUser.setDelFlag("0");
        return dingUserMapper.list(dingUser);
    }

    /**
     * 新增钉钉组织-用户
     * 
     * @param dingUser 钉钉组织-用户
     * @return 结果
     */
    @Override
    public int insert(DingUser dingUser)
    {
        dingUser.setCreateTime(DateUtils.getNowDate());
        return dingUserMapper.insert(dingUser);
    }
    /**
     * 批量新增钉钉组织-用户
     *
     * @param dingUserList 钉钉组织-用户
     * @return 结果
     */
    @Override
    public int insertBatch(List<DingUser> dingUserList)
    {
        return dingUserMapper.insertBatch(dingUserList);
    }
    /**
     * 修改钉钉组织-用户
     * 
     * @param dingUser 钉钉组织-用户
     * @return 结果
     */
    @Override
    public int update(DingUser dingUser)
    {
        dingUser.setUpdateTime(DateUtils.getNowDate());
        return dingUserMapper.update(dingUser);
    }

    /**
     * 批量删除钉钉组织-用户
     * 
     * @param ids 需要删除的钉钉组织-用户主键
     * @return 结果
     */
    @Override
    public int deleteBatch(Long[] ids)
    {
        return dingUserMapper.deleteBatch(ids);
    }

    /**
     * 删除钉钉组织-用户信息
     * 
     * @param id 钉钉组织-用户主键
     * @return 结果
     */
    @Override
    public int delete(Long id)
    {
        return dingUserMapper.delete(id);
    }

    /**
     * 导入钉钉组织-用户信息
     *
     * @param list 钉钉组织-用户信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<DingUser> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            DingUserMapper dingUserMapper = sqlSession.getMapper(DingUserMapper.class);
            list.stream().forEach(item -> dingUserMapper.insert(item));
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
